package api.cart;

import bean.Goods;
import db.GoodsMockTable;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();
        response.setContentType("text/html;charset=utf-8");
        if(session.getAttribute("user")==null){//如果session新创建的，则表示这个用户还没有登录，是刚开始访问的
            String msg="您还没有登录呢！三秒后自动跳转到登录页面！";
            response.getWriter().write(msg);
            response.setHeader("Refresh","3;URL=/login.html");
        }else{
            Goods goods5= GoodsMockTable.getUserByName("苹果手机5");
            Goods goods3= GoodsMockTable.getUserByName("苹果手机3");
            List<Goods> goodses=new ArrayList<Goods>();
            goodses.add(goods3);
            goodses.add(goods5);
            session.setAttribute("cart",goodses);

            Cookie cookie=new Cookie("JSESSIONID",session.getId());
            response.addCookie(cookie);
            response.getWriter().write("<a href='/pay'>你已经选购了两件商品，点击我进入支付页面</a><br/>");
            response.getWriter().write("<a href='/logout'>退出登录</a>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
